package com.example.spark.controller;

import com.example.spark.entity.Bottom;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.BottomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author gky
 * @date 2023/08/26 15:09
 * @description
 */
@CrossOrigin
@RestController
@RequestMapping("/bottom")
public class BottomController {
    @Autowired
    private BottomService bottomService;

    /**
     * 查询底部信息(数据库中只有一条信息)
     * @return 底部信息
     */
    @GetMapping("/query")
    public CommonResult<Bottom> getBottom(){
        return bottomService.getBottom();
    }


    /**
     * 添加底部信息
     * @param bottom 底部信息json
     * @return 成功与否
     */
    @PostMapping("/save")
    public CommonResult<String> save(@RequestBody Bottom bottom){
        return bottomService.updateBottom(bottom);
    }

    /**
     * 修改底部信息
     * @param bottom 底部信息json
     * @return 成功与否
     */
    @PutMapping("/updateBottom")
    public CommonResult<String> updateBottom(@RequestBody Bottom bottom){
        return bottomService.updateBottom(bottom);
    }

    /**
     * 修改图片
     * @param photoName 图片字段名
     * @param file 图片
     * @return
     */
    @PutMapping("/updateImg/{photoName}")
    public CommonResult<String> updateImg(@PathVariable String photoName, @RequestPart MultipartFile file){
        return bottomService.updateImg(photoName,file);
    }

    /**
     * 根据id删除底部信息
     * @param id 底部信息id
     * @return 成功与否
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Long id){
        if (bottomService.removeById(id)){
            return new CommonResult<>(200,"success","恭喜你，删除成功！");
        }else {
            return new CommonResult<>(200,"error","删除失败");
        }
    }


}
